<template>
  <div class="app-container">

    <el-container direction="vertical">
      <el-main >
        <el-card class="box-card">
          <div v-for="(item,index) in specs" :key="index" style="margin-bottom: 25px">
            <span style="margin-right: 10px">{{item.name}}：</span>
            <el-checkbox v-for="(attr,index1) in item.get_attr" :label="attr.name" :key="index1" @click.native="getSelectAttr($event,index,index1)"></el-checkbox>
            <div style="float: right">
              <el-input style="width: 200px;height: 5px;margin-left: 30px"  v-model="item.new" placeholder="属性" ></el-input>
              <el-button style="margin-left: 10px"  type="primary" @click="addAttr(index)">添加</el-button>
              <!-- <el-button style="margin-left: 10px"  type="danger" @click="delAttr(index)">删除</el-button> -->
            </div>
          </div>
        </el-card>
      </el-main>


      <el-main>
         <!-- <el-table :data="tableData" style="width: 100%">
            <el-table-column  v-for="(item,index) in combindAttr"  :key="index" label="日期"  width="180"></el-table-column>
          </el-table> -->
      </el-main>
    </el-container>




  </div>
</template>

<script>
import { myGet } from '@/utils/myUtils'

const  urlGetSpecLst = '/guard/shop/goods/spec/lst'
const  urlGetAttrAdd = '/guard/shop/goods/attr/add'

export default {
  data(){
    return {
      selectAttr:[],
      combindAttr:null,
      input:'',
      specs:[]
    }
  },
  methods:{
    addAttr(index){
      const name = this.specs[index].new
      const spec_id = this.specs[index].id
      myGet(urlGetAttrAdd,{name,spec_id}).then(res=>{
        if(res.status){

            this.specs[index].get_attr.push({
              value:res.data.records.id,
              name:res.data.records.name
            })
        }
      })
    },

    getSpec(){
        myGet(urlGetSpecLst).then(res=>{
          if(res.status){
            if(res.data.records.length>0){
              this.specs = res.data.records
            }
          }
        })
    },
    getSelectAttr(e,index,index1){
        if (e.target.tagName !== 'SPAN') return
        if(this.selectAttr[index]==undefined){
          this.selectAttr[index] = []
        }

        this.selectAttr[index].push({
          attrId:this.specs[index].id,
          attrName:this.specs[index].name,
          attrValueId:this.specs[index].get_attr[index1].id,
          attrValueName:this.specs[index].get_attr[index1].name
        })


        const info = this.getProducts(this.selectAttr)
        //console.log(info)
        let res = []
        info.forEach(item=>{
          let temp = []
          item.forEach(val=>{
            temp.push({
              attr_id:val.attrValueId,
              attr_name:val.attrValueName
            })
          })
          res.push(temp)
        })
        console.log(res)




    },
        // 笛卡儿积
    getProducts(specs) {
      if (!specs || specs.length == 0) {
        return [];
      } else {
        return joinSpec([
          []
        ], specs, 0, specs.length - 1);
      }

      function joinSpec(prevProducts, specs, i, max) {
        var currentProducts = [],
          currentProduct, currentSpecs = specs[i];
        if (i > max) {
          return prevProducts;
        }
        prevProducts.forEach(function (prevProduct) {
          currentSpecs.forEach(function (spec) {
            currentProduct = prevProduct.slice(0);
            currentProduct.push(spec);
            currentProducts.push(currentProduct);
          });
        });
        return joinSpec(currentProducts, specs, ++i, max);
      }
    },




  },
  mounted() {
    this.getSpec()
  },
}
</script>

<style scoped>

</style>
